'use strict';
var relay;
var relay2;
var Server = require('home').Server;
var server = new Server();
var Client = require('cmcc_sms').Client;

var keys = {
    'api': '5a4269e8bcea434f8d321d631f467f9c',
    'secret': 'acefc4dbebde4c088ccb8244d0bfc96a'
};

var client = new Client(keys);

var options = {
    'sign': 'nanchao',
    'mobile': '15618228112',
    'id': 1378
};

function sendMsg(params) {
    options.parameters = params;
    client.send(options, function (error) {
        if (error) {
            console.log(error);
            return;
        }
    
        console.log('send successfully');
    });
}


server.get('/turn-on-relay', function(req, res) {
    relay.turnOn(function() {
        console.log('turn on relay');
    });
    sendMsg(['左边的灯']);
    res.end();
});

server.get('/turn-off-relay', function (req, res) {
    relay.turnOff(function() {
        console.log('turn off relay');
    });
    res.end();
});

server.get('/turn-on-relay2', function(req, res) {
    relay2.turnOn(function() {
        console.log('turn on relay2');
    });
    sendMsg(['右边的灯']);
    res.end();
});

server.get('/turn-off-relay2', function (req, res) {
    relay2.turnOff(function() {
        console.log('turn off relay2');
    });
    res.end();
});

$.ready(function (error) {
    if (error) {
        console.log(error);
        return;
    }
    relay = $("#relay");
    relay2 = $("#relay2");
    server.listen(6318);

    $('#led-r').turnOn();    
});

$.end(function () {
    $('#relay').turnOff();
    $('#relay2').turnOff();
    $('#led-r').turnOff();
});
